﻿$(function () {
    $("#divSpecial").hide();
    $("#divload").hide();
    $("#RdUnSPecial").attr("checked", true);
})
var i = 0;
var inputTxt = 0;
//添加小组成员
function addMember() {
    if ($("#txtgroupName").attr("readOnly") == true) {
        $("#dl_member").append("<dd id=\"member" + i + "\">学号：<input  type=\"text\"  style=\"border:1px solid #ccc; margin-top:3px;\"/><label id=\"lb_member" + i + "\"></label><a id=\"a_member" + i + "\" href=\"javascript:;\" onclick=\"check('" + i + "')\">确认</a>|<a href=\"javascript:;\" onclick=\"delMember('member" + i + "')\" >删除</a></dd>");
        i++;
    }
    else {
        checkGroupName();
    }
}
//删除单个小组成员
function delMember(id) {
    if ($("#" + id).children("input").attr("readOnly") == true)
        inputTxt = inputTxt - 1;
    $("#" + id).remove();
}
//删除所有小组成员
function delAll() {
    $("#dl_member").children("dd").remove();
    inputTxt = 0;
}
//检测学生学号是否已报此竞赛
function check(number) {
    var ConId = $("#ConId").html();
    var StuId = $("#member" + number).children("input").val();
    if (StuId != "") {
        var parameter = "{'StuId':'" + StuId + "','ConId':'" + ConId + "'}";
        var load = "<img src=\"../../images/loding.gif\" id=\"lb_member" + number + "\"  alt=\"\" />";
        var ssurl = "moreSignUp.aspx/CheckStuId";
        var replb = "<label id=\"lb_member" + number+ "\"></label>";
        $.ajax({
            type: "POST",     //提交方式
            contentType: "application/json; charset=utf-8",   //内容类型
            dataType: "json",     //类型
            url: ssurl,    //提交的页面，方法名
            data: parameter,    //参数，如果没有，可以为null
            beforeSend: function () { $("#lb_member" + number).replaceWith(load); }, //发送数据之前，显示
            //            complete: function () {  }, //接受数据完毕
            success: function (data) { //如果执行成功，那么执行此方法
                $("#lb_member" + number).replaceWith(replb);
                var result = eval("(" + data.d + ")");
                if (result.resultjson == "1") {
                    var rep = "<span id=\"lb_member" + number + "\"><img src=\"../../images/error.gif\"  alt=\"\" /><span class='error'>该用户已经报名  </span></span>";
                    $("#lb_member" + number).replaceWith(rep);
                }
                else if (result.resultjson == "0") {
                    var rep = "<span id=\"a_member" + number + "\"><img src=\"../../images/true.gif\"  alt=\"\" /></span>";
                    $("#a_member" + number).replaceWith(rep);
                    $("#lb_member").replaceWith(replb);
                    $("#member" + number).children("input").attr("readOnly", true);
                    inputTxt = inputTxt + 1;
                }
                else if (result.resultjson == "2") {
                    var rep = "<span id=\"lb_member" + number + "\"><img src=\"../../images/error.gif\"  alt=\"\" /><span class='error'>此学号不合法  </span></span>";
                    $("#lb_member" + number).replaceWith(rep);
                }
            },
            error: function (err) { //如果执行不成功，那么执行此方法
                alert("err:" + err);
                return -1;
            }
        });
    }
    else {
        var rep = "<span id=\"lb_member" + number + "\"><img src=\"../../images/error.gif\"  alt=\"\" class='error' /><span class='error'>学号不允许为空  </span></span>";
        $("#lb_member" + number).replaceWith(rep);
    }
}
//检测小组名称是否可用
function checkGroupName() {
    var groupName = $("#txtgroupName").val();
    if (groupName != "") {
        var ConId = $("#ConId").html();
        var ssurl = "moreSignUp.aspx/checkGroupName"
        var parameter = "{'groupName':'" + groupName + "','ConId':'" + ConId + "'}";
        var load = "<img src=\"../../images/loding.gif\"  id=\"groupCheck\" alt=\"\" />"
        $.ajax({
            type: "POST",     //提交方式
            contentType: "application/json; charset=utf-8",   //内容类型
            dataType: "json",     //类型
            url: ssurl,    //提交的页面，方法名
            data: parameter,    //参数，如果没有，可以为null
            beforeSend: function () { $("#groupCheck").replaceWith(load); }, //发送数据之前，显示
            success: function (data) { //如果执行成功，那么执行此方法
                var result = eval("(" + data.d + ")");
                if (result.resultjson == "1") {
                    var rep = "<span id=\"groupCheck\"><img src=\"../../images/true.gif\"  alt=\"\" />该组名可用</span>";
                    $("#groupCheck").replaceWith(rep);
                    var repDel = "<a href='javascript:;' id=\"Acheck\" onclick = 'empty(\"txtgroupName\")'>修改</a>"
                    $("#txtgroupName").attr("readOnly", true);
                    $("#Acheck").replaceWith(repDel);
                }
                else if (result.resultjson == "0") {
                    var rep = "<span id=\"groupCheck\"><img src=\"../../images/error.gif\"  alt=\"\" /><span class=\"error\">该组名已经被占用</span><span>";
                    $("#groupCheck").replaceWith(rep);
                }
            },
            error: function (err) { //如果执行不成功，那么执行此方法
                alert("err:" + err);
                return -1;
            }
        });
    }
    else {
        var rep = "<span id=\"groupCheck\"><img  src=\"../../images/error.gif\"  alt=\"\" /><span class=\"error\">组名不允许为空</span><span>";
        $("#groupCheck").replaceWith(rep);
    }
}
//清空内容
function empty(Name) {
    $("#" + Name).attr("readOnly", false).val("");
    var rep = "<a href=\"javascript:;\"  onclick=\"checkGroupName()\" id=\"Acheck\">验证小组名</a>";
    $("#Acheck").replaceWith(rep);
    var repLb = "<label id=\"groupCheck\"></label>"
    $("#groupCheck").replaceWith(repLb);
}
//确定参赛按钮点击
function SignUp() {
    //即所有的学号都检验过了
    if (($("#dl_member dd").length != 0) && ($("#dl_member dd").length == inputTxt)) {
        var PAR_CONTESTS = $("#ConId").html();
        var GRO_NAME = $("#txtgroupName").val();
        var STU_CODE = "";
        $("#dl_member dd").children("input").each(function () {
            STU_CODE += $(this).val();
            STU_CODE += " ";
        })
        var parameter = "STU_CODE=" + STU_CODE + "&GRO_NAME=" + GRO_NAME + "&PAR_CONTESTS=" + PAR_CONTESTS + "&GRO_SPECIALTY=" + checkCon($("#RdUnSPecial").attr("checked"), true, 0, 1);
        var tbody = "";
        $.ajax({
            type: "POST", //用POST方式传输
            dataType: "json", //数据格式:JSON
            url: 'MoreSignUp.ashx', //目标地址
            data: parameter,
            beforeSend: function () { $("#divload").show();  }, //发送数据之前,显示loading gif,隐藏数据
            complete: function () { $("#divload").hide();  }, //接收数据完毕
            success: function (json) {
                $("#productTable tr:gt(0)").remove();
                var productData = json.tempTable;
                $.each(productData, function (i, n) {
                    var trs = "";
                    trs += "<tr><td>" + n.STU_CODE + "</td><td>" + n.STU_NAME + "</td><td>" + n.CLA_NAME + "</td><td>" + n.ACA_NAME + "</td><td>" + n.CON_NAME + "</td><td>" + checkCon(n.ACA_NAME, "1", "专业组", " 非专业组") + "</td></tr>";
                    tbody += trs;
                });

                $("#productTable").append(tbody);
                $("#divSpecial").show();
            },
            error: function (e) {
                alert(e.responseText);
            }
        });
    }
    else if ($("#dl_member dd").length == 0) {
        alert("请输入参赛的学号");
    }
    else
        alert("请确保所有的学号已被检测");
}
//条件检验值
//checkStr——输入的值
//trueResult——真正的值
//returnTrue——如果为真，应返回的结果
//returnFalse——如果为家，应返回的结果
function checkCon(checkStr, trueResult, returnTrue, returnFalse) {
    if (checkStr == trueResult) {
        return returnTrue;
    }
    else
        return returnFalse;
}

